﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Hartley.ApplicationLogic;
using Hartley.DTOs;

namespace WebApplication1
{
    public partial class viewAllHeads : System.Web.UI.Page
    {
        protected int currentPageNumber = 1;
        private const int PAGE_SIZE = 10;
        AdminAL adminAL;
        StudentAL studentAL;
        MemberAL memberAL;
        SportsAL sportsAL;
        string selectedHeadType; //i.e all principal, all ppa president, all current staff, all prefects
        protected void Page_Load(object sender, EventArgs e)
        {
            string qrystring= Request.QueryString.ToString();
            //When you call this page you use 
            //selectedHeadType="AllPrincipals"
            //selectedHeadType="AllVicePrincipals"
            //selectedHeadType="AllSectionalHeads"
            //selectedHeadType="AllHeadPrefects"
            //selectedHeadType="AllDeputyHeadPrefects"
            //selectedHeadType="AllStaffs"
            //selectedHeadType="AllCurrentStaffs"
            //selectedHeadType="AllCMBPPAPresidents"
            //selectedHeadType="AllCMBPPASecretaries"
            //selectedHeadType="AllCMBPPATreasurers"
            //selectedHeadType="AllCMBPPAPatrons"
            //selectedHeadType="AllCMBPPAVicePatrons"
            //selectedHeadType="AllCMBPPAVicePresidents"
            //selectedHeadType="AllCMBPPACommitteeMembers"
            //selectedHeadType="AllCricketCaptains"
            //selectedHeadType="AllFootballCaptains"
            //selectedHeadType="AllVolleyBallCaptains"
            //selectedHeadType="AllCricketViceCaptains"
            //selectedHeadType="AllFootballViceCaptains"
            //selectedHeadType="AllVolleyBallViceCaptains"
            //selectedHeadType="AllCricketCoaches"
            //selectedHeadType="AllFootballCoaches"
            //selectedHeadType="AllVolleyBallCoaches"
            //selectedHeadType="AllCricketTeamMembers"
            //selectedHeadType="AllFootballTeamMembers"
            //selectedHeadType="AllVolleyBallTeamMembers"

            selectedHeadType = qrystring.Substring(qrystring.LastIndexOf('=')+1);
            Session["selectedHeadType"] = selectedHeadType;
            if (!IsPostBack)
            {
                BindData(selectedHeadType);
            }

        }


        private void BindData(string selectedHeadType)
        {

            adminAL = new AdminAL();
            studentAL = new StudentAL();
            memberAL = new MemberAL();
            sportsAL = new SportsAL();

            List<Object> allHeads = new List<Object>();
            List<Object> allHeadsOfOnePage = new List<Object>();

            if (selectedHeadType.Equals("AllPrincipals"))
            {
                allHeads = adminAL.GetAllStaff().Where(a => a.Role == "Principal").OrderByDescending(c=>c.TeachFrom).Select(b=>b as Object).ToList();
            }
            else if (selectedHeadType.Equals("AllVicePrincipals"))
            {
                allHeads = adminAL.GetAllStaff().Where(a => a.Role == "Vice Principal").OrderByDescending(c=>c.TeachFrom).Select(b => b as Object).ToList();
            }
            else if (selectedHeadType.Equals("AllStaffs"))
            {
                allHeads = adminAL.GetAllStaff().OrderByDescending(c=>c.TeachFrom ).Select(b => b as Object).ToList();
            }
            else if (selectedHeadType.Equals("AllCurrentStaffs"))
            {
                allHeads = adminAL.GetAllStaff().Where(a => a.IsCurrentStaff ).Select(b => b as Object).ToList();
            }
            else if (selectedHeadType.Equals("AllSectionalHeads"))
            {
                allHeads = adminAL.GetAllStaff().Where(a => a.Role == "Sectional Head").OrderByDescending(c=>c.TeachFrom).Select(b => b as Object).ToList();
            }
            else if (selectedHeadType.Equals("AllHeadPrefects"))
            {
                allHeads = studentAL.GetAllPrefects().Where(a => a.Role == "Head Prefect").OrderByDescending(c=>c.PrefectFrom).Select(b => b as Object).ToList();
            }
            else if (selectedHeadType.Equals("AllDeputyHeadPrefects"))
            {
                allHeads = studentAL.GetAllPrefects().Where(a => a.Role == "Deputy Head Prefect").OrderByDescending(c=>c.PrefectFrom).Select(b => b as Object).ToList();
            }
            else if (selectedHeadType.Equals("AllCMBPPAPresidents"))
            {
                allHeads = memberAL.GetAllCommitteeMembers().Where(a => a.Position == "President").OrderByDescending(c=>c.MemberFrom).Select(b => b as Object).ToList();
            }
            else if (selectedHeadType.Equals("AllCMBPPASecretaries"))
            {
                allHeads = memberAL.GetAllCommitteeMembers().Where(a => a.Position == "Secretary").OrderByDescending(c => c.MemberFrom).Select(b => b as Object).ToList();
            }
            else if (selectedHeadType.Equals("AllCMBPPATreasurers"))
            {
                allHeads = memberAL.GetAllCommitteeMembers().Where(a => a.Position == "Treasurer").OrderByDescending(c => c.MemberFrom).Select(b => b as Object).ToList();
            }
            else if (selectedHeadType.Equals("AllCMBPPAPatrons"))
            {
                allHeads = memberAL.GetAllCommitteeMembers().Where(a => a.Position == "Patron").OrderByDescending(c => c.MemberFrom).Select(b => b as Object).ToList();
            }
            else if (selectedHeadType.Equals("AllCMBPPAVicePatrons"))
            {
                allHeads = memberAL.GetAllCommitteeMembers().Where(a => a.Position == "Vice Patron").OrderByDescending(c => c.MemberFrom).Select(b => b as Object).ToList();
            }
            else if (selectedHeadType.Equals("AllCMBPPAVicePresidents"))
            {
                allHeads = memberAL.GetAllCommitteeMembers().Where(a => a.Position == "Vice President").OrderByDescending(c => c.MemberFrom).Select(b => b as Object).ToList();
            }
            else if (selectedHeadType.Equals("AllCMBPPACommitteeMembers"))
            {
                allHeads = memberAL.GetAllCommitteeMembers().Where(a => a.Position == "Committee Member").OrderByDescending(c => c.MemberFrom).Select(b => b as Object).ToList();
            }
           


            // get the total rows 
            double totalRows = (int)allHeads.Count;

            lblAllHeadsPage.Text = "Displaying  - Page ";
            lblAllHeadsOf.Text = "  of  ";

            lblAllHeadsTotalPage.Text = CalculateTotalPages(totalRows).ToString();

            lblAllHeadsCurrPage.Text = currentPageNumber.ToString();

            int totalPageNumber = lblAllHeadsTotalPage.Text == "" ? 0 : Convert.ToInt32(lblAllHeadsTotalPage.Text);

            if (totalPageNumber == 1)
            {
                if (currentPageNumber == 1)
                {
                    btnAllHeadsFirst.Enabled = false;
                    btnAllHeadsPrev.Enabled = false;
                    btnAllHeadsNext.Enabled = false;
                    btnAllHeadsLast.Enabled = false;
                    allHeadsOfOnePage = allHeads;
                }

            }
            else if (totalPageNumber > 1)
            {
                if (currentPageNumber == 1)
                {
                    btnAllHeadsFirst.Enabled = false;
                    btnAllHeadsPrev.Enabled = false;
                    btnAllHeadsNext.Enabled = true;
                    btnAllHeadsLast.Enabled = true;
                    allHeadsOfOnePage = allHeads.GetRange(0, PAGE_SIZE);
                }
                else if (currentPageNumber < totalPageNumber && currentPageNumber > 1)
                {
                    btnAllHeadsFirst.Enabled = true;
                    btnAllHeadsPrev.Enabled = true;
                    btnAllHeadsNext.Enabled = true;
                    btnAllHeadsLast.Enabled = true;
                    allHeadsOfOnePage = allHeads.GetRange(((currentPageNumber - 1) * PAGE_SIZE), PAGE_SIZE);

                }
                else if (currentPageNumber == totalPageNumber)
                {
                    btnAllHeadsFirst.Enabled = true;
                    btnAllHeadsPrev.Enabled = true;
                    btnAllHeadsNext.Enabled = false;
                    btnAllHeadsLast.Enabled = false;
                    allHeadsOfOnePage = allHeads.GetRange(((currentPageNumber - 1) * PAGE_SIZE), (allHeads.Count - (currentPageNumber - 1) * PAGE_SIZE));
                }
            }
            

            CreateDynamicTable(allHeadsOfOnePage,selectedHeadType);


        }

        private int CalculateTotalPages(double totalRows)
        {
            int totalPages = (int)Math.Ceiling(totalRows / PAGE_SIZE);

            return totalPages;
        }

        protected void ChangePage(object sender, CommandEventArgs e)
        {

            switch (e.CommandName)
            {

                case "First":
                    currentPageNumber = 1;
                    break;

                case "Previous":
                    currentPageNumber = Int32.Parse(lblAllHeadsCurrPage.Text) - 1;
                    break;

                case "Next":
                    currentPageNumber = Int32.Parse(lblAllHeadsCurrPage.Text) + 1;
                    break;
                case "Last":
                    currentPageNumber = Int32.Parse(lblAllHeadsTotalPage.Text);
                    break;
            }


            BindData(Session["selectedHeadType"].ToString());
        }


        private void CreateDynamicTable(List<Object> allHeadsOfOnePage, string selectedHeadType)
        {
            PlaceHolder4.Controls.Clear();

            // Fetch the number of Rows and Columns for the table 
            // using the properties


            int tblRows = allHeadsOfOnePage.Count;

            List<StudentDTO> studentDTO = new List<StudentDTO>();
            List<StaffDTO> staffDTO = new List<StaffDTO>();
            List<PrefectDTO> prefectDTO = new List<PrefectDTO>();
            List<MemberDTO> memberDTO = new List<MemberDTO>();
            List<CommitteeMemberDTO> committeeMemberDTO = new List<CommitteeMemberDTO>();

            // Create a Table and set its properties 
            Table tbl = new Table();
            tbl.Width = new Unit("100%");
            tbl.BorderStyle = BorderStyle.Solid;
            // Add the table to the placeholder control
            PlaceHolder4.Controls.Add(tbl);

            TableRow trHeader = new TableRow();
            trHeader.Width = new Unit("25%");

            HyperLink hlHeadHeaderName = new HyperLink();
            hlHeadHeaderName.Text = "Name";

            HyperLink hlHeadHeaderDesignation = new HyperLink();
            hlHeadHeaderDesignation.Text = "Current Designation";

            HyperLink hlHeadHeaderFrmYr = new HyperLink();
            hlHeadHeaderFrmYr.Text = "From";

            HyperLink hlHeadHeaderToYr = new HyperLink();
            hlHeadHeaderToYr.Text = "To";


            List<HyperLink> allHeaderHyperlinks = new List<HyperLink>();

            //based  on selected type change column orders

            allHeaderHyperlinks.Add(hlHeadHeaderName);
            allHeaderHyperlinks.Add(hlHeadHeaderDesignation);
            allHeaderHyperlinks.Add(hlHeadHeaderFrmYr);
            allHeaderHyperlinks.Add(hlHeadHeaderToYr);


            

            foreach (HyperLink hyperlinkHeader in allHeaderHyperlinks)
            {
                TableCell tcHeader = new TableCell();
                tcHeader.BackColor = System.Drawing.Color.DarkSlateBlue;
                tcHeader.ForeColor = System.Drawing.Color.White;

                tcHeader.Controls.Add(hyperlinkHeader);
                trHeader.Cells.Add(tcHeader);



            }

            tbl.Rows.Add(trHeader);

            TableRow tr = null;
            // Now iterate through the table and add your controls 
            for (int i = 0; i < tblRows; i++)
            {
                tr = new TableRow();
                tr.Width = new Unit("25%");

                HyperLink hlHeadCellName = new HyperLink();
                HyperLink hlHeadCellDesignation = new HyperLink();
                HyperLink hlHeadCellFrmYr = new HyperLink();
                HyperLink hlHeadCellToYr = new HyperLink();

                if (selectedHeadType.Equals("AllPrincipals") || selectedHeadType.Equals("AllVicePrincipals") ||
                    selectedHeadType.Equals("AllStaffs") || selectedHeadType.Equals("AllCurrentStaffs") ||
                    selectedHeadType.Equals("AllSectionalHeads"))
                {
                    staffDTO = adminAL.GetStaffById(allHeadsOfOnePage.Select(a => a as StaffDTO).ToList()[i].StaffId);
                    hlHeadCellName.Text = staffDTO[0].FullName;
                    hlHeadCellName.NavigateUrl = "viewStaffById.aspx?" + staffDTO[0].StaffId;
                    hlHeadCellDesignation.Text = staffDTO[0].Role;

                    hlHeadCellFrmYr.Text = staffDTO[0].TeachFrom.Value.ToShortDateString();

                    if (staffDTO[0].IsCurrentStaff == true)
                    {
                        hlHeadCellToYr.Text = "Till Now";
                    }
                    else
                    {
                        hlHeadCellToYr.Text = staffDTO[0].TeachTo.Value.ToShortDateString();
                    }
                }
                else if (selectedHeadType.Equals("AllHeadPrefects") || selectedHeadType.Equals("AllDeputyHeadPrefects"))
                {
                    //List<StudentDTO> stdntDTO = allHeadsOfOnePage.Select(a => a as StudentDTO).ToList();
                    prefectDTO = studentAL.GetPrefectsById(allHeadsOfOnePage.Select(a => a as PrefectDTO).ToList()[i].StudentId);
                    studentDTO = studentAL.GetStudentById(prefectDTO[0].StudentId);

                    hlHeadCellName.Text = studentDTO[0].FullName;
                    hlHeadCellName.NavigateUrl = "viewStudentById.aspx?" + studentDTO[0];
                    hlHeadCellDesignation.Text = prefectDTO[0].Role;

                    hlHeadCellFrmYr.Text = prefectDTO[0].PrefectFrom.Value.ToShortDateString();
                    hlHeadCellToYr.Text = prefectDTO[0].PrefectTo.Value.ToShortDateString();



                }

                else if (selectedHeadType.Equals("AllCMBPPAPresidents") || selectedHeadType.Equals("AllCMBPPASecretaries") ||
                         selectedHeadType.Equals("AllCMBPPATreasurers") || selectedHeadType.Equals("AllCMBPPAPatrons")     ||
                         selectedHeadType.Equals("AllCMBPPAVicePatrons") || selectedHeadType.Equals("AllCMBPPAVicePresidents") ||
                selectedHeadType.Equals("AllCMBPPACommitteeMembers") )
                {
                    //List<StudentDTO> stdntDTO = allHeadsOfOnePage.Select(a => a as StudentDTO).ToList();
                    committeeMemberDTO = memberAL.getCommitteeMemberById(allHeadsOfOnePage.Select(a => a as CommitteeMemberDTO).ToList()[i].MemberId);
                    memberDTO = memberAL.GetMemberById(committeeMemberDTO[0].MemberId);

                    hlHeadCellName.Text = memberDTO[0].FullName;
                    hlHeadCellName.NavigateUrl = "viewAllRolesOfCommMember.aspx?" + memberDTO[0].MemberId;
                    hlHeadCellDesignation.Text = committeeMemberDTO[0].Position;

                    hlHeadCellFrmYr.Text = committeeMemberDTO[0].MemberFrom.Value.ToShortDateString();
                    hlHeadCellToYr.Text = committeeMemberDTO[0].MemberTo.Value.ToShortDateString();



                }

               

                List<HyperLink> allHyperlinks = new List<HyperLink>();
                allHyperlinks.Add(hlHeadCellName);
                allHyperlinks.Add(hlHeadCellDesignation);
                allHyperlinks.Add(hlHeadCellFrmYr);
                allHyperlinks.Add(hlHeadCellToYr);


            

                // int j=0;  
                foreach (HyperLink hyperlink in allHyperlinks)
                {
                    TableCell tc = new TableCell();



                    if (i % 2 == 0)
                    {
                        tc.Style.Add("background-color", "White");
                        tc.ForeColor = System.Drawing.Color.LightSeaGreen;
                        //tc.BackColor = System.Drawing.Color.WhiteSmoke;
                    }
                    else
                    {
                        tc.Style.Add("background-color", "#003399");
                        tc.ForeColor = System.Drawing.Color.WhiteSmoke;
                        //tc.BackColor = System.Drawing.Color.LightSeaGreen;
                    }



                    // Add the control to the TableCell
                    tc.Controls.Add(hyperlink);


                    // Add the TableCell to the TableRow
                    tr.Cells.Add(tc);
                }

                // Add the TableRow to the Table

                tbl.Rows.Add(tr);
            }

            // This parameter helps determine in the LoadViewState event,
            // whether to recreate the dynamic controls or not

            ViewState["dynamictable"] = true;
        }
    }
}